package com.hsgene.sample.controller;

import com.hsgene.common.RangeType;
import com.hsgene.common.util.result.ApiResult;
import com.hsgene.common.util.result.NullObject;
import com.hsgene.common.util.support.LimitHelper;
import com.hsgene.common.util.support.OrderHelper;
import com.hsgene.product.exception.ProductErrorCode;
import com.hsgene.sample.condition.SampleCondition;
import com.hsgene.sample.constants.TypeLabel;
import com.hsgene.sample.domain.GeneticTestingSample;
import com.hsgene.sample.dto.GeneticTestingAgencySampleDto;
import com.hsgene.sample.dto.GeneticTestingSampleDto;
import com.hsgene.sample.service.SampleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc:样本管理
 * @author: fuxiang
 * @date: 2018/8/14 13:58
 */
@RestController
@RequestMapping("/v1.0/genetic/testing/sample")
public class SampleController {

    @Resource
    private SampleService sampleService;


    /**
     * y样本列表
     *
     * @param page
     * @param size
     * @param condition 查询参数
     * @param rangeType 区别是否是全部信息
     * @param typeLabel 区别是样本类型还是样本值
     * @return
     * @author FX
     * @date 2018/10/31 17:39
     */
    @GetMapping
    public ApiResult queryList(Integer page, Integer size
            , SampleCondition condition
            , @RequestParam(name = "rangeType", required = false) String rangeType
           ) {
            if (StringUtils.isNotEmpty(rangeType) && RangeType.ALL.getMessage().equals(rangeType)) {
              return sampleService.getAll(condition);
            } else {
                condition.setLimitHelper(LimitHelper.create(page, size, true));
                condition.setOrderHelper(OrderHelper.create("update_date_time", false));
                return sampleService.queryList(condition);
            }
    }


    /**
     * 查询ById
     *
     * @return
     */
    @GetMapping("{id}")
    public ApiResult<GeneticTestingSampleDto> queryById(@PathVariable("id") String id, String typeLabel) {
        return sampleService.queryById(id,typeLabel);
    }

    /**
     * 插入样笨
     *
     * @param geneticTestingSample
     * @return
     */
    @PostMapping
    public ApiResult<NullObject> insertSampleDto(@RequestBody GeneticTestingSample geneticTestingSample) {
        return sampleService.insertSampleDto(geneticTestingSample);
    }

    /**
     * 根据名称重复检验
     *
     * @param label
     * @param id
     * @return
     * @author FX
     * @date 2018/10/31 16:43
     */
    @GetMapping("name")
    public ApiResult getByName(String label, String id,String typeLabel) {
        GeneticTestingSample label1 = sampleService.getByLabel(label,typeLabel);
        if (StringUtils.isNotEmpty(id)) {
            GeneticTestingSample one = sampleService.getByIdAndNoDelete(id);
            if (one != null && one.getLabel().equals(label)) {
                return ApiResult.succ();
            }
        }
        if (label1 == null) {
            return ApiResult.succ();
        }
        return ApiResult.fail(ProductErrorCode.EXISTED);
    }

    /**
     * 修改样笨
     *
     * @param geneticTestingSample
     * @return
     */
    @PutMapping("{id}")
    public ApiResult<NullObject> updateSampleDto(@PathVariable("id") String id, @RequestBody GeneticTestingSample geneticTestingSample) {
        geneticTestingSample.setId(id);
        return sampleService.updateSampleDto(geneticTestingSample);
    }


    /**
     * 删除样笨
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public ApiResult<NullObject> deleteSampleDto(@PathVariable("id") String id) {
        GeneticTestingSample geneticTestingSample = new GeneticTestingSample();
        geneticTestingSample.setId(id);
        return sampleService.deleteSampleDto(geneticTestingSample);
    }


    /**
     * 批量删除样笨
     *
     * @param
     * @return
     */
    @DeleteMapping
    public ApiResult<NullObject> deleteSampleDtoMore(@RequestBody List<GeneticTestingSample> sampleList) {
        return sampleService.deleteSampleDtoMore(sampleList);
    }


    /**
     * 根据ids获取样本
     *
     * @return
     */
    @GetMapping("sampleList")
    public ApiResult<List<GeneticTestingSampleDto>> getSamplesByids(@RequestParam("ids") String ids) {
        return sampleService.getSamplesByids(ids);
    }

    /**
     * 更新样本呗使用的数量
     *
     * @param id
     * @param type add或reduce
     * @return
     */
    @PutMapping("used/amount")
    public ApiResult updateIsUsedAmount(String id, String type) {
        return sampleService.updateIsUsedAmount(id, type);
    }

}
